package com.test.ssm.controller;

import com.test.ssm.pojo.AdminUser;
import com.test.ssm.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author 徒有琴
 */
@Controller
public class SystemController {
    @RequestMapping("{page}.html")
    public String index(@PathVariable String page) {
        return page;
    }
//    @RequestMapping("password.html")
//    public String password(){
//        return "password";
//    }

    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping("dologin.html")
    public String doLogin(String name, String password, Model model, HttpSession session) {
        AdminUser adminUser = adminUserService.getUserByName(name);
        if (adminUser == null || !adminUser.getPassword().equals(password)) {
            model.addAttribute("message","用户名密码错误");
            return "login";
        }
        session.setAttribute("session_user",adminUser);
        return "redirect:index.html";
    }

    @RequestMapping("logout.html")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:login.html";
    }
}
